﻿using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace RaptorEngine.Components.Drawable
{
	public class DrawableText : Drawable2DComponent
	{
		private SpriteFont _font;

        public DrawableText(Entity e, SpriteFont font, string text) 
            : base(e, text)
        {
            _font = font;
        }
        public DrawableText(Entity e, SpriteFont font, string text, Color color, float alpha)
            : this(e, font, text)
        {
            Alpha = alpha;
            Color = color;
        }
        public DrawableText(Entity e, SpriteFont font, string text, Color color, float alpha, bool centerText)
            : this(e, font, text, color, alpha)
        {
            if(centerText)
                CenterText();
        }

        public override void Draw(SpriteBatch spritebatch)
        {
            spritebatch.DrawString(
                _font,
                textString,
                new Vector2(Entity.Position.X, Entity.Position.Y),
                Color * Alpha);
        }

        public void CenterText()
        {
            Vector2 size = Vector2.Zero;

            if (_font != null && !string.IsNullOrEmpty(TextString))
            {
                size = _font.MeasureString(TextString);

                Vector2 pos = size * new Vector2(0.5f, 0.5f);
 
                Vector3 newPosition = new Vector3(pos.X,pos.Y, 0);

                Entity.Position -= newPosition;
            }
        }
	}
}
